package com.dianrui.operation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.operation.R;
import com.dianrui.operation.bean.PlanningRecordList;
import com.dianrui.operation.view.MyListView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PlanningRecordAdapter extends BaseAdapter {

    private Context context;
    private List<PlanningRecordList> planningRecordLists;



    public PlanningRecordAdapter(Context context, List<PlanningRecordList> planningRecordLists) {
        this.context = context;
        this.planningRecordLists = planningRecordLists;
    }

    public void refresh(List<PlanningRecordList> planningRecordLists) {
        this.planningRecordLists = planningRecordLists;
        notifyDataSetChanged();
    }



    @Override
    public int getCount() {
        return planningRecordLists.size();
    }

    @Override
    public Object getItem(int position) {
        return planningRecordLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_planningrecord, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (planningRecordLists != null && planningRecordLists.size() > 0) {
            final PlanningRecordList planningRecordList = planningRecordLists.get(position);
            if(position==0){
                holder.dateLayout.setVisibility(View.VISIBLE);
                holder.dateTitle.setText(planningRecordList.date_title);
                holder.dateTitle.setVisibility(View.VISIBLE);
            } else if(planningRecordLists.get(position).date_title.equals(planningRecordLists.get(position-1).date_title)){
                holder.dateTitle.setVisibility(View.GONE);
                holder.dateLayout.setVisibility(View.GONE);
            }else {
                holder.dateTitle.setText(planningRecordList.date_title);
                holder.dateLayout.setVisibility(View.VISIBLE);
                holder.dateTitle.setVisibility(View.VISIBLE);
            }

            final PlanningRecordListAdapter planningRecordListAdapter=new PlanningRecordListAdapter(context);
            planningRecordListAdapter.refresh(planningRecordList.data);
            holder.listView.setAdapter(planningRecordListAdapter);

        }

        return convertView;
    }

    class ViewHolder {

        @BindView(R.id.date_title)
        TextView dateTitle;
        @BindView(R.id.listview)
        MyListView listView;
        @BindView(R.id.date_layout)
        LinearLayout dateLayout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
